package com.ctjsoft.threadstatus;

/**
 * 测试线程的wait和sleep
 * 其中wait是Object类的实例方法，任何Object类的实例都可以调用wait方法。
 * sleep是Thread类的静态方法，使用Thread.sleep()来调用。
 *
 * @author: liaoyy
 * @date: Created in 2021/11/12 15:45
 */
public class WaitAndSleepDemo {

    public static void main(String[] args) {


        final Object object = new Object();
        try {
            new Thread(() -> {
                System.out.println("线程A开始");
                synchronized (object) {
                    try {
                        System.out.println("线程A获取到锁");
                        object.wait(1000);
                        System.out.println("线程A结束");
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }).start();

            Thread.sleep(100);

            new Thread(() -> {
                System.out.println("线程B开始");
                synchronized (object) {
                    try {
                        System.out.println("线程B获取到锁");
                        Thread.sleep(1000);
                        System.out.println("线程B结束");
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }).start();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

}

